package com.originskyseed.platform.common.api.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;

public class smsUtil {

	// 发送短信
	public static String sendSms(String secretId, String secretKey, String sdkAppId, String signName, String temPlateId, String[] phoneNumberSet, String[] templateParamSet) {
		String ret = "";
		try {
			Credential cred = new Credential(secretId, secretKey);
			HttpProfile httpProfile = new HttpProfile();
			httpProfile.setEndpoint("sms.tencentcloudapi.com");
			ClientProfile clientProfile = new ClientProfile();
			clientProfile.setHttpProfile(httpProfile);
			SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

			SendSmsRequest req = new SendSmsRequest();
			req.setPhoneNumberSet(phoneNumberSet);
			req.setSmsSdkAppId(sdkAppId);
			req.setSignName(signName);
			req.setTemplateId(temPlateId);
			req.setTemplateParamSet(templateParamSet);
			SendSmsResponse resp = client.SendSms(req);
			JSONObject json = JSONObject.parseObject(SendSmsResponse.toJsonString(resp));
			if (json.containsKey("SendStatusSet")) {
				JSONArray jsonArray = json.getJSONArray("SendStatusSet");
				if (jsonArray.size() > 0) {
					ret = jsonArray.getJSONObject(0).getString("Code");
				}
			}
			System.out.println(SendSmsResponse.toJsonString(resp));
		} catch (TencentCloudSDKException e) {
			System.out.println(e.toString());
		}
		return ret;
	}
}
